package com.atguigu.distributed.lock.leecode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTreeErgodic {
    //非递归方式遍历前序二叉树
    public void preOrderWithStack(TreeNode root){
        TreeNode treeNode = root;
        Stack<TreeNode> stack = new Stack<>();
        while (treeNode!=null||!stack.isEmpty()){
            while (treeNode!=null){
                System.out.println(treeNode.data);
                stack.push(treeNode);
                treeNode = treeNode.left;
            }
            if(!stack.isEmpty()){
                 treeNode = stack.pop();
                 treeNode = treeNode.right;
            }
        }
    }
    public void levelTraversal(TreeNode root){
        TreeNode treeNode = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            System.out.println(node.data);
            TreeNode left = node.left;
            TreeNode right = node.right;
            if(left!=null){
                queue.offer(left);
            }
            if(right!=null){
                queue.offer(right);
            }
        }
    }
    
}
